﻿using Syncfusion.PMML;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using static senlv.Controllers.HomeController;

namespace senlv.Models
{
    public class RecommendHelper
    {
        public class pmmlPredictionResult
        {
            public string PredictedValue { get; set; }
            public string PredictedProbability0 { get; set; }
            public string PredictedProbability1 { get; set; }
            public string PredictedProbability2 { get; set; }
            public string PredictedProbability3 { get; set; }
            public string PredictedProbability4 { get; set; }
            public string PredictedProbability5 { get; set; }
            public string PredictedProbability6 { get; set; }
        }
        public double BooltoDouble(string input)
        {
            if (input == "true")
                return 1.0;
            return 0.0;
        }
        public pmmlPredictionResult pmmlPrediction(recommend_json recommend_Json)
        {
            var anonymousType = new
            {
                b = BooltoDouble(recommend_Json.b),
                c = BooltoDouble(recommend_Json.c),
                d = BooltoDouble(recommend_Json.d),
                e = BooltoDouble(recommend_Json.e),
                f = BooltoDouble(recommend_Json.f),
                g = BooltoDouble(recommend_Json.g),
                h = BooltoDouble(recommend_Json.h),
                i = BooltoDouble(recommend_Json.i),
                j = BooltoDouble(recommend_Json.j),
                k = BooltoDouble(recommend_Json.k),
                l = BooltoDouble(recommend_Json.l),
                m = BooltoDouble(recommend_Json.m),
                n = BooltoDouble(recommend_Json.n),
                o = BooltoDouble(recommend_Json.o),
                p = BooltoDouble(recommend_Json.p),
                q = BooltoDouble(recommend_Json.q),
                r = BooltoDouble(recommend_Json.r),
                s = BooltoDouble(recommend_Json.s),
                t = BooltoDouble(recommend_Json.t),
                u = BooltoDouble(recommend_Json.u)
            };
            //string pmmlFilePath = "../../Tips.pmml";
            
            //string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\lib\\村落推荐_逻辑回归二分类-Model.pmml");
       // Stream stream = new FileStream(filePath, FileMode.Open);
            Stream stream = new FileStream("C:\\senlv\\Release\\wwwroot\\lib\\村落推荐_逻辑回归二分类-Model.pmml", FileMode.Open);
            // Stream stream = new FileStream("C:\\Users\\PC\\Desktop\\机器学习\\vr4.pmml", FileMode.Open);
            //Create instance for PMML Document            
            PMMLDocument pmmlDocument = new PMMLDocument(stream);
            stream.Close();
            //Create instance for Mining model            
            RegressionModelEvaluator regressionModel = new RegressionModelEvaluator(pmmlDocument);
            
            //Gets the predicted result            
            PredictedResult predictedResult = regressionModel.GetResult(anonymousType, null);
             regressionModel.Dispose();
            //Displays the predicted result    
            //Console.WriteLine("Predicted is:" + predictedResult.PredictedValue);
            //Console.WriteLine("'老周三村0'PredictedProbability:" + predictedResult.GetPredictedProbability("0"));
            //Console.WriteLine("'迈德村1'PredictedProbability:" + predictedResult.GetPredictedProbability("1"));
            //Console.WriteLine("'三卿村2'PredictedProbability:" + predictedResult.GetPredictedProbability("2"));
            //Console.WriteLine("'莲塘村3'PredictedProbability:" + predictedResult.GetPredictedProbability("3"));
            //Console.WriteLine("'王下乡4'PredictedProbability:" + predictedResult.GetPredictedProbability("4"));
            //Console.WriteLine("'荣堂村5'PredictedProbability:" + predictedResult.GetPredictedProbability("5"));
            //Console.WriteLine("'保平村6'PredictedProbability:" + predictedResult.GetPredictedProbability("6"));
            pmmlPredictionResult result = new pmmlPredictionResult();
            result.PredictedValue = predictedResult.PredictedValue.ToString();
            result.PredictedProbability0 = String.Format("{0:F}", predictedResult.GetPredictedProbability("0"));
            result.PredictedProbability1 = String.Format("{0:F}", predictedResult.GetPredictedProbability("1"));
            result.PredictedProbability2 = String.Format("{0:F}", predictedResult.GetPredictedProbability("2"));
            result.PredictedProbability3 = String.Format("{0:F}", predictedResult.GetPredictedProbability("3"));
            result.PredictedProbability4 = String.Format("{0:F}", predictedResult.GetPredictedProbability("4"));
            result.PredictedProbability5 = String.Format("{0:F}", predictedResult.GetPredictedProbability("5"));
            result.PredictedProbability6 = String.Format("{0:F}", predictedResult.GetPredictedProbability("6"));
            return result;
        }
    }
}
